import os
import sys

# 添加项目根目录到Python路径，以便导入config_reader模块
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(current_dir))
sys.path.append(project_root)

from config_reader import get_mem0_api_key
from mem0 import MemoryClient

# 从配置文件获取API密钥
api_key = get_mem0_api_key()

# 使用 MemoryClient 替代 Memory
client = MemoryClient(api_key=api_key)

messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."},
    {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]

# 使用 MemoryClient 添加记忆
client.add(messages, user_id="alice", metadata={"category": "movies"})

# 搜索相关记忆
query = "What type of movies do I like?"
search_results = client.search(query, user_id="alice")
print("搜索结果:", search_results)

# 获取用户的所有记忆
filters = {
    "AND": [
        {
            "user_id": "alice"
        }
    ]
}
all_memories = client.get_all(version="v2", filters=filters, page=1, page_size=50)
print("所有记忆:", all_memories)